<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class menu extends Admin_Controller {
	var $model = "mmenu";
	var $view = "menu";
	
	function __construct() {
        parent::__construct();
        $this->load->model($this->model);
        $this->url_index = admin_url() . $this->view . '/index';
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
		$this->load->model('mcategory');
    }
	
	public function index($menu = 0) {
        $data = array();
		$post = $this->input->post();
		if (isset($post['menu'])) {
			$post['menu'] = (int) $post['menu'];
			redirect($this->url_index .'/'. $post['menu']);
		}
		$data['parents'] = $this->{$this->model}->key_value($this->{$this->model}->get_parent(), 'men_name');
		$data['parents'][''] = 'None';

		$menu = (int) $menu;
		$data['menu_id'] = $menu;
		if ($menu) 
			$data['edit'] = $this->{$this->model}->get($menu);
			
		$data['menu_tree'] = $this->{$this->model}->get_menu($menu);
		$data['cates'] = $this->mcategory->get_cat_tree();

		$this->template_data['main'] = 'admin/'.$this->view."/index";
		$this->template->load('layout/admin/layout',$this->template_data,$data);
    }
	
	public function edit($editID = 0) {
        $data = array();
        $post = $this->input->post();
        $data['edit'] = $this->{$this->model}->get($editID);
        if ($post) {
			if ($editID > 0) {
				$post['edit']['cat_time_create'] = time();
                $this->{$this->model}->update($editID, $this->{$this->model}->primaryKey, $post['edit']);
            }
			else{
				$editID = $this->{$this->model}->insert($post['edit']);
			}
        }
		
		redirect($this->url_index .'/'. $editID);
	}
	
	public function edit_child() {
		$result = array(
			'status' => 0,
			'msg' => 'Chưa xử lí yêu cầu'
		);
		$data = array();
		$data['men_name'] = $this->input->post('men_name');
		$data['men_type'] = $this->input->post('men_type');
		$data['men_value'] = $this->input->post('men_value');
		$men_id =  $this->input->post('men_id');
		if (
			$data['men_name']
			&& $data['men_type']
			&& $data['men_value']
		) {
			if ($men_id) {
				//update
				$this->update($this->input->post(), $men_id);
				
				$result['status'] = 1;
				$result['msg'] = 'Đã cập nhật';
			} else {
				//add
				$id = $this->add($this->input->post());
				$result['status'] = 2;
				$result['msg'] = 'Đã thêm';
				$result['men_id'] = $id;
			}
		}else {
			$result['status'] = -1;
			$result['msg'] = 'Không đủ tham số';
		}
		echo json_encode($result);
		die;
	}
	


	function add_url($parent = 0){
		$name = $this->input->post("men_name");
		$link = $this->input->post("men_link");
		$type = $this->input->post("men_type");
		$parent = $this->input->post("men_parent");
		$id = (int)$this->input->post("men_id");
		if ($id > 0) {
			$status = (in_array($this->input->post('men_status'),array(0,1))) ? $this->input->post('men_status') : 1;
			if (!$parent) $parent = 0;
			$this->mmenu->update($id, 'men_id', array(
			'men_name'=>$name,
			// 'men_type'=>$type,
			'men_parent'=>$parent,
			'men_value'=>$link,
			'men_status'=>$status,
			));
		}else {
			$this->db->order_by('men_order desc');
			$men = $this->mmenu->get($parent, 'men_parent');
			
			$data_update = array(
			'men_name'=>$name,
			'men_type'=>$type,
			'men_parent'=>$parent,
			'men_value'=>$link,
			);
			if ($men) {
				$data_update ['men_order'] = $men->men_order+1;
			}
			$id = $this->mmenu->insert($data_update);
		}
		echo $id;
	}


	/**
	 * [save_order description]
	 * @param  integer
	 * @return [type]
	 */
	function save_order($menu_id = 0) {
		if (!$menu_id) 
			die;
		$json = $this->input->post("json");
		$object = json_decode($json, true);
		$data_update = array();
		$this->{$this->model}->update_recursive($object, $menu_id);
		echo $json;
		die;
	}
	
	function del_child($menu_id = 0) {
		$menu_id = (int) $menu_id;
		if ($menu_id > 0){
			$this->{$this->model}->delete($menu_id);
		}
		die;
	}
	
	function import_from_cate() {
		
		$this->load->model('mcategory');
		$tree = $this->mcategory->get_cat_tree_array(true);
		
		$__parent = 1;
		if ($tree) foreach ($tree as $cate) {
			if (!isset($cate['parent']) || !$cate['parent'])
				continue;
			$parent =  (array) $cate['parent'];
			
			$id_parent = $this->mmenu->insert(array(
				'men_name' =>$parent['cat_name'],
				'men_value'=>$parent['cat_id'],
				'men_parent'=>$__parent,
				'men_type'=>'cat',
				));
			
			if (isset($cate['child']) && $cate['child']) {
				foreach ($cate['child'] as $child) {
					$child = (array) $child;
					$id_child = $this->mmenu->insert(array(
					'men_name' =>$child['cat_name'],
					'men_value'=>$child['cat_id'],
					'men_parent'=>$id_parent,
					'men_type'=>'cat',
					));
				}
			}
		}
		
	}
}